package io.geekidea.boot.shop.controller.web;

import io.geekidea.boot.auth.annotation.Permission;
import io.geekidea.boot.common.enums.SysLogType;
import io.geekidea.boot.framework.annotation.Log;
import io.geekidea.boot.framework.page.Paging;
import io.geekidea.boot.framework.response.ApiResult;
import io.geekidea.boot.shop.dto.TemplateDto;
import io.geekidea.boot.shop.query.web.TemplateQuery;
import io.geekidea.boot.shop.service.base.api.TemplateService;
import io.geekidea.boot.shop.vo.web.TemplateVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 网页模板表 控制器
 *
 * @author geekidea
 * @since 2024-10-27
 */
@Slf4j
@RestController
@Tag(name = "网页模板表")
@RequestMapping("/admin/template")
public class TemplateController {

    @Autowired
    private TemplateService templateService;

    /**
     * 添加网页模板表
     *
     * @param dto
     * @return
     * @throws Exception
     */
    @Log(type = SysLogType.ADD)
    @Operation(summary = "添加网页模板表")
    @PostMapping("/addTemplate")
    @Permission("template:add")
    public ApiResult addTemplate(@Valid @RequestBody TemplateDto dto) {
        log.info("添加网页模板表：{}", dto);
        boolean flag = templateService.addTemplate(dto);
        return ApiResult.result(flag);
    }

    /**
     * 修改网页模板表
     *
     * @param dto
     * @return
     * @throws Exception
     */
    @Log(type = SysLogType.UPDATE)
    @Operation(summary = "修改网页模板表")
    @PostMapping("/updateTemplate")
    @Permission("template:update")
    public ApiResult updateTemplate(@Valid @RequestBody TemplateDto dto) {
        log.info("修改网页模板表：{}", dto);
        boolean flag = templateService.updateTemplate(dto);
        return ApiResult.result(flag);
    }

    /**
     * 删除网页模板表
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Log(type = SysLogType.DELETE)
    @Operation(summary = "删除网页模板表")
    @PostMapping("/deleteTemplate/{id}")
    @Permission("template:delete")
    public ApiResult deleteTemplate(@PathVariable String id) {
        log.info("删除网页模板表：{}", id);
        boolean flag = templateService.deleteTemplate(id);
        return ApiResult.result(flag);
    }

    /**
     * 获取网页模板表详情
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Operation(summary = "获取网页模板表详情")
    @PostMapping("/getTemplate/{id}")
    @Permission("template:info")
    public ApiResult<TemplateVo> getTemplate(@PathVariable String id) {
        log.info("获取网页模板表详情：{}", id);
        TemplateVo templateVo = templateService.getTemplateById(id);
        return ApiResult.success(templateVo);
    }

    /**
     * 获取网页模板表分页列表
     *
     * @param query
     * @return
     * @throws Exception
     */
    @Operation(summary = "获取网页模板表分页列表")
    @PostMapping("/getTemplatePage")
    @Permission("template:page")
    public ApiResult<TemplateVo> getTemplatePage(@Valid @RequestBody TemplateQuery query) {
        log.info("获取网页模板表分页列表：{}", query);
        Paging<TemplateVo> paging = templateService.getTemplatePage(query);
        return ApiResult.success(paging);
    }

}
